<!--
* description: 财务管理-手工上账财务审核
* author: wangjuan
* createdDate: 2021-3-2
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-more-btn="true"
      :dynamic-is-show-select="true"
      :dynamic-is-init-table="true"
      :callback-obj="callbackObj"
    />
  </div>
</template>
<script>

import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import Vue from 'vue'
import api from '@/api/pa/financialManagement'

export default {
  name: 'ManualBookedFianceAudit',

  components: {
    OneTableTemplate
  },
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      selectData: [],
      // 网格查询API配置对象
      apiConfig: api.manualQueryByPage,

      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'),
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset')
        },
        {
          compKey: 'btnKey3',
          size: 'small',
          name: 'reject',
          icon: 'iconfont icon-reject',
          clickEvent: () => this.reject(),
          text: '驳回',
          position: 'left',
          disabled: true
        },
        {
          compKey: 'btnKey4',
          size: 'small',
          name: 'adopt',
          icon: 'iconfont icon-adopt',
          clickEvent: () => this.pass(),
          text: '通过',
          position: 'left',
          disabled: true
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey2',
              labelName: '网点编码：',
              codeField: 'dlrCode',
              textField:'dlrShortName',
              component: () => import('@/components/pa/dlrCode'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '网点名称：',
              codeField: 'dlrShortName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              disabled: true,
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey1',
              labelName: '网点类型：',
              codeField: 'dlrTypeName',
              component: () => import('@/components/org/commonInput'),
              // oFields: 'dlrType,dlrTypeName',
              // customObj: {
              //   api: api.queryDropDown,
              //   listQuery: {
              //     dlrType: ''
              //   },
              //   selectType: '1'
              // },
              type: 'dropdownList',
              isMul: false,
              isMust: true,
              disabled: true
            },
            {
              compKey: 'compKey4',
              labelName: '到账日期：',
              codeField: 'arrivalDate',
              component: () => import('@/components/org/datePicker/datePicker'),
              type: 'datePicker',
              isMul: false,
              isMust: true
            }
          ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'dlrId',
              label: '专营ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'accountReviewId',
              label: '审核ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'dlrCode',
              label: '网点编码'
            },
            {
              prop: 'dlrShortName',
              label: '网点名称'
            },
            {
              prop: 'dlrTypeName',
              label: '网点类型'
            },
            {
              prop: 'postAmount',
              label: '上账金额'
            },
            {
              prop: 'arrivalDate',
              label: '到账日期'
            },
            {
              prop: 'remark',
              label: '备注'
            }
          ],

      // 表单查询数据
      formField: this.$utils.getFormField(this, {
        dlrType: '',
        dlrTypeName: '',
        dlrCode: '',
        dlrShortName: '',
        updateControlId: '',
        dlrId: '',
        accountReviewId: '',
        arrivalDate: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode]),

      // 单表格模版页面组件回调事件
      callbackObj: {
        // 当选择项发生变化时会触发该事件（多选时）
        onCheckSelectChange: (selection) => {
          this.selectData = selection

          this.handleSelectionChange(selection)
        }
      }
    }
  },

  mounted() {
    // 列表查询
    this.queryTable(1)
  },

  methods: {
    // 通过
    pass() {
      // debugger
      this.$confirm('确定要通过吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const datas = []
        this.selectData.forEach((val, index) => {
          datas.push({
            dlrType: val.dlrType,
            dlrTypeName: val.dlrTypeName,
            dlrCode: val.dlrCode,
            dlrShortName: val.dlrShortName,
            arrivalDate: val.arrivalDate,
            dlrId: val.dlrId,
            accountReviewId: val.accountReviewId,
            updateControlId: val.updateControlId,
            postAmount: val.postAmount
          })
        })
        const queryObj = {
        // 保存需要传 type="mutation"
          type: 'mutation',
          apiConfig: api.manualReview,
          // 条件/实体参数（变量），根据typeParam中的定义配置
          variables: {
            dataInfo: datas
          }
        }
        // 转换了中台请求格式数据
        var paramD = this.$getParams(queryObj)
        this.$requestAPI(paramD).then((response) => {
          if (response.result === '1') {
            this.$message({
              message: response.msg
            })
          } else {
            this.$message({
              message: response.msg
            })
          }
          this.queryTable(1)
        })
      }).catch(err => {
        console.log(err)
      })
    },

    // 驳回
    reject() {
      // debugger
      this.$confirm('确定要驳回吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const datas = []
        this.selectData.forEach((val, index) => {
          datas.push({
            dlrType: val.dlrType,
            dlrTypeName: val.dlrTypeName,
            dlrCode: val.dlrCode,
            dlrShortName: val.dlrShortName,
            arrivalDate: val.arrivalDate,
            dlrId: val.dlrId,
            accountReviewId: val.accountReviewId,
            updateControlId: val.updateControlId
          })
        })
        const queryObj = {
        // 保存需要传 type="mutation"
          type: 'mutation',
          apiConfig: api.manualReject,
          // 条件/实体参数（变量），根据typeParam中的定义配置
          variables: {
            dataInfo: datas
          }
        }
        // 转换了中台请求格式数据
        var paramD = this.$getParams(queryObj)
        this.$requestAPI(paramD).then((response) => {
          if (response.result === '1') {
            this.$message({
              message: response.msg
            })
          } else {
            this.$message({
              message: response.msg
            })
          }
          this.queryTable(1)
        })
      }).catch(err => {
        console.log(err)
      })
    },

    // 当选择项发生变化时会触发该事件
    handleSelectionChange(selection) {
      const btns = this.tableButtons.filter(o => o.disabled !== undefined)
      if (btns.length > 0) {
        const isCheck = selection && selection.length > 0
        btns.forEach((item) => {
          item.disabled = !isCheck
        })
      }
    }
  },

  watch:{
    'formField.dlrType':{
      handler(val){
        if(val==="1"){
          this.formField.dlrTypeName="直营店"
        }else if(val==="2"){
          this.formField.dlrTypeName="授权店"
        }else{
          this.formField.dlrTypeName=""
        }
      }   
    }
  }
}
</script>
